import random

max_reputation = 0.5
prob_margin = 0.0
predicted_hunts = 0
predicted_hunts_corrector = 0.0
cooperation_count = 0
choices_count = 0

def hunt_choices(round_number, current_food, current_reputation, m,  player_reputations):
	
	global max_reputation
	global prob_margin	
	global predicted_hunts
	global predicted_hunts_corrector
	global cooperation_count
	global choices_count
	
	
	if round_number < 3:
		hunt_decisions = ['h' for x in player_reputations]
		return hunt_decisions
	
	####keep your reputation as high as the highest player_reputation
	if current_reputation < max_reputation:
		#cooperate more
		prob_margin += 0.1 
	else:
		#cooperate less
		prob_margin -= -0.05
	
	
	####predict how many hunts on this round
	players_count = 0
	predicted_hunts = 0
	max_reputation = 0
	#	1) count how many players are left and get max reputation
	for x in player_reputations:
		if x > max_reputation:
			max_reputation = x
		players_count += 1
	#	2)predict how many hunts for this round
	for x in player_reputations:
		predicted_hunts += players_count*x
		
	max_hunts = m - predicted_hunts
	if max_hunts < 0:
		max_hunts = 0
	
	####Calculate predicted food gain for this round
	#	x = cooperaion ration =>> how likely are players to cooperate with me
	x = 0
	if choices_count > 0 :
		x = cooperation_count/ choices_count
	#	h = the number of hunts I need to gain the bonus points
	h = max_hunts
	#	p = the number of choices i have for this turn
	p = players_count
	#	s = how many stalls if i play 'h' hunts
	s = p - h
	#	b = bonus points
	b = p*2
	#	profit = estimated food gain assuming I hunt 'h' times and get the bonus
	profit = h*(x*0 + (1-x)*(-3)) + (p - h)*(x*1+(1-x)*(-2)) + b
	
	####Calculate my_reputation after if I hunt 'h' times and stall the rest
	p_hunts = (round_number -1)*current_reputation
	p_stalls = (round_number -1) - p_hunts
	
	new_reputation = (p_hunts + h)/(p_hunts + h  + p_stalls + s)
	
	####Make hunt decision
	hunt_decisions = list()
 	if current_reputation > max_reputation*(.8) and new_reputation > max_reputation*(.7)   and profit > 0 :
 		#	cooperate with top 'h' players
 		pr_list_copy  =list(player_reputations)
 		pr_list_copy.sort(reverse=True)
 		low_reputation = pr_list_copy[h]
 		
 		for x in player_reputations:
 			if x >=low_reputation :
 				hunt_decisions.append('h')
			else:
				hunt_decisions.append('s')

	else:
		
		for x in player_reputations:
			ran = random.random()		
			adj_dec_boundary = x*(1 + prob_margin)
			if ran < adj_dec_boundary:
				hunt_decisions.append('h')
			else:
				hunt_decisions.append('s')

		
	return hunt_decisions



def hunt_outcomes(food_earnings):
	
	global cooperation_count
	global choices_count
	

	#count how many times players cooperate
	for x in food_earnings:
		if x >= 0:
			cooperation_count += 1
		choices_count += 1

    	

def round_end(award, m, number_hunters):
	
	pass
